package com.yupi.springbootinit.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static com.yupi.springbootinit.constant.RabbitMqConstant.*;

/**
 * @author wzh
 */
@Configuration
public class OrderRabbitMqConfig {


    @Bean(ORDER_SUBMIT_QUEUE)
    public Queue orderQueue(){
    //绑定死信队列，将废弃的消息丢入死信队列
        Map<String, Object> args = new HashMap<>(2);
        // 绑定我们的死信交换机
        args.put("x-dead-letter-exchange", EXCHANGE_ORDER_DLX_EXCHANGE);
        // 绑定我们的路由key
        args.put("x-dead-letter-routing-key", ROUTINGKEY_DLX_ORDER_PAY);
        // 过期时间为30分钟
        args.put("x-message-ttl", 30*60000);

        return new Queue(ORDER_SUBMIT_QUEUE,true,false,false,args);
    }

    @Bean(ORDER_SUBMIT_EXCHANGE)
    public Exchange orderExchange(){
        return new DirectExchange(ORDER_SUBMIT_EXCHANGE,true,false);
    }


    @Bean
    public Binding BING_QUEUE_ORDER_SUBMIT_QUEUE(){
        return new Binding(ORDER_SUBMIT_QUEUE,
                Binding.DestinationType.QUEUE,
                ORDER_SUBMIT_EXCHANGE,ORDER_SUBMIT_ROUTE_KEY,null);
    }


    /** 声明死信队列 */
    @Bean(QUEUE_ORDER_DLX_QUEUE)
    public Queue QUEUE_ORDER_DLX_QUEUE(){
        return new Queue(QUEUE_ORDER_DLX_QUEUE,true,false,false);
    }


  /** 声明死信交换机 */
    @Bean(EXCHANGE_ORDER_DLX_EXCHANGE)
    public Exchange EXCHANGE_ORDER_DLX_EXCHANGE(){
        return new DirectExchange(EXCHANGE_ORDER_DLX_EXCHANGE,true,false);
    }


 /** 死信交换机绑定死信队列 */
    @Bean
    public Binding BINDING_DLX_QUEUE_ORDER_PAY(){
        return new Binding(QUEUE_ORDER_DLX_QUEUE,
                Binding.DestinationType.QUEUE, EXCHANGE_ORDER_DLX_EXCHANGE,
                ROUTINGKEY_DLX_ORDER_PAY,null);
    }

}
